package tp.pr4.gui;

import tp.pr4.RobotEngine;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import tp.pr4.Rotation;
import tp.pr4.instructions.*;

import javax.swing.*;

/**
 * This class creates the window for the Swing interface. The MainWindow contains the following components: �A menu with the QUIT action
 * 
 * �An Action panel with several buttons to perform MOVE, TURN, OPERATE, PICK, and DROP actions. Additionally it has a combo box of turn 
 * rotations and a text field to write the name of the item that the robot wants to pick from the current place
 * 
 * �A RobotPanel that displays the robot information (fuel and recycled material) and the robot inventory, which shows a table with item 
 * names and descriptions that the robot carries. The user can select the items contained in the inventory in order to DROP or OPERATE an item
 * 
 * �A CityPanel that represents the city. It shows the places that the robot has visited and an icon that represents the robot heading. 
 * The robot heading is updated when the user performs a TURN action. The visible places are updated when the robot performs a MOVE action.
 * Once a place is visited, the user can click on it in order to display the place description (similar to the RADAR command). 
 * @author Alvaro Saez Hernando
 * @author Noel Clemente Montero 
 */
public class MainWindow extends JFrame {
	
	/**
	 * Creates the window and the panels using Swing Components. It stores a reference to the RobotEngine object and provides the 
	 * panels to the robot engine in order to communicate the simulation events.
	 * @param robot The RobotEngine that receives the instructions performed by the action panel
	 */
	private static final long serialVersionUID = 1L;
	private RobotEngine _robot;
	
	public MainWindow(RobotEngine robot){
		
		super("WALL·E");
		_robot = robot;
		
		this.setPreferredSize(new Dimension(720, 600));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		

		JPanel mainPanel = new JPanel(new BorderLayout());

		/*
		 * Barra de menu
		 */
		JMenuBar mbMenu = new JMenuBar();
		this.setJMenuBar(mbMenu);

		JMenu mDialogs = new JMenu("File");
		mbMenu.add(mDialogs);
		initializeMenu(mDialogs);
		
		/*
		 * Paneles
		 */
		
		//Instructions panel
		JPanel instructionsPanel = new JPanel();
		GridLayout layout = new GridLayout(0,2);
		instructionsPanel.setLayout(layout);
		JButton btMove = new JButton("MOVE");
		instructionsPanel.add(btMove);
		btMove.addActionListener(new ActionListener(){
			public void actionPerformed( ActionEvent evento){
				Instruction instruction = new MoveInstruction();
				_robot.communicateRobot(instruction);}
		});
		
		//Quit button
		JButton btQuit = new JButton("QUIT");
		instructionsPanel.add(btQuit);
		btQuit.addActionListener(new ActionListener() {
			   public void actionPerformed( ActionEvent evento ){quitWindows();}}); 
	
		//Turn button
		JButton btTurn = new JButton("TURN");
		instructionsPanel.add(btTurn);
		
		//Turn combo box
		JComboBox cbDirection = new JComboBox();
		cbDirection.addItem(Rotation.RIGHT);
		cbDirection.addItem(Rotation.LEFT);
		instructionsPanel.add(cbDirection);
		
		//Pick button
		JButton btPick = new JButton("PICK");
		instructionsPanel.add(btPick);
		
		//Pick text area
		JTextArea object = new JTextArea();
		instructionsPanel.add(object);
		
		//Drop button
		JButton btDrop = new JButton("DROP");
		instructionsPanel.add(btDrop);
		
		//Operate button
		JButton btOperate = new JButton("OPERATE");
		instructionsPanel.add(btOperate);
		
		//Robot Panel
		JPanel robotPanel = new RobotPanel();
		
		//Navigation Panel
		JPanel navigationPanel = new NavigationPanel();
		
		JSplitPane SuperiorPanel = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
				instructionsPanel, robotPanel);
		mainPanel.add(SuperiorPanel, BorderLayout.NORTH);
		
		JSplitPane inferiorPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT, 
				SuperiorPanel, navigationPanel);
		mainPanel.add(inferiorPanel, BorderLayout.CENTER);
		
		
		
		
		
		this.add(mainPanel);
	}
	
	
	/**
	 * carga la barra de menu
	 * @param mDialogs
	 */
	private void initializeMenu(JMenu mDialogs) {
	
		JMenuItem mQuit = new JMenuItem("Quit");
		mDialogs.add(mQuit);
		mQuit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				quitWindows();
			}//actionPerformed
		});//addActionListener
	}//initializeMenu
	
	/**
	 * Cierra el programa
	 */
	private void quitWindows (){
		int option = JOptionPane.showConfirmDialog(rootPane, "De verdad desea salir?", "Confirmar",JOptionPane.YES_NO_OPTION);
		if (option == JOptionPane.YES_OPTION){
			JOptionPane.showMessageDialog(null, "Game Over");
			System.exit(0);
		}//if
	}
	
	/*private void moveRobot(RobotEngine robot){
		robot.setGUIWindow(this);
	}*/
}
